//
//  SCJCycleImages.m
//  06-CycleImages
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SCJCycleImages.h"
@interface SCJCycleImages () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *cyclePageControll;


@end
@implementation SCJCycleImages
#pragma mark - initWithFrame
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imagesArray = [NSArray array];
        // *** 子控件初始化时frame不要与初始化方法的frame一样 ***
        // 推荐使用self.bounds / CGRectMake(0, 0, frame.size.width, frame.size.height);
        _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self initCycleScrollView];
        [self creatImagePageControll:frame];
        
    }
    return self;
}
- (void)initCycleScrollView {
    _cycleScrollView.bounces = NO;
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.delegate = self;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    // 因为添加了最后一张图要让轮播图看起来在第一张
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    [self addSubview:_cycleScrollView];
}
#warning 后期需要添加page的位置改变, 这里默认位置
#pragma mark - creatPageControll
- (void)creatImagePageControll:(CGRect)frame {
    _cyclePageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 30)];
    _cycleScrollView.backgroundColor = [UIColor grayColor];
    _cyclePageControll.numberOfPages = 5;
    _cyclePageControll.center = CGPointMake(frame.size.width / 2, frame.size.height - 30 / 2);
    [self addSubview:_cyclePageControll];
    [_cyclePageControll addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark - changeContentoffset
- (void)imagePageChange:(UIPageControl *)page {
    [_cycleScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
 }
#pragma mark - ImageArray
- (void)addImagesArrayWith:(nonnull NSArray *)array {
    //根据传过来的数组,进行处理,生成图片数组
    NSArray *imagesArray = [self handleArray:array];
    // 在逻辑判断时需要用到数组中的内容
    self.imagesArray = imagesArray;
    // 使用数组创建ImageView
    [self createAllImageView:imagesArray];
    // 根据图片数组,设置对应的视图
    _cycleScrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
    _cyclePageControll.numberOfPages = array.count;
}
#pragma mark - handleImageArray
- (NSArray *)handleArray:(NSArray *)array {
    // 判断类型, 不知道别人使用你的类的时候传进来的类型是什么
    id firstObject = [array firstObject];
    NSMutableArray *imagesArray = [NSMutableArray array];
    // 判断是不是某种类型 isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        //把字符串数组转成图片数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imagesArray setArray:array];
    }
    UIImage *firstImage = [imagesArray firstObject];
    UIImage *lastImage = [imagesArray lastObject];
    [imagesArray insertObject:lastImage atIndex:0];
    [imagesArray addObject:firstImage];
    return [imagesArray copy];
}
#pragma mark - creatScrollView
- (void)createAllImageView:(NSArray *)imagesArray {
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width * i, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}
#pragma mark - ScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    //改变page的下标
    _cyclePageControll.currentPage = page - 1;
    if (page == 0) {
        // 改变移动视图的偏移量
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        _cyclePageControll.currentPage =  _cyclePageControll.numberOfPages - 1;
    } else if (page == _imagesArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * 1, 0);
        _cyclePageControll.currentPage = 0;
    }
}



@end
